namespace ConsoleApp3._1028;

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;


public class UserServiceImpl : UserService
{
    private List<User> userList = new List<User>();
    
    public bool Register(string username, string password, string confirmPassword)
    {
        if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("用户名和密码不可为空！");
            return false;
        }
        
        username = username.Trim();
        password = password.Trim();
        
        if (!ValidateUsername(username))
        {
            Console.WriteLine("用户名格式错误！长度必须为3-15个字符，只能包含字母、数字、下划线");
            return false;
        }
        
        if (!ValidatePassword(password))
        {
            Console.WriteLine("密码格式错误！长度必须为6-20个字符，必须包含至少1个大写字母、1个小写字母和1个数字");
            return false;
        }
        
        if (password != confirmPassword)
        {
            Console.WriteLine("两次输入的密码不一致！");
            return false;
        }
        
        foreach (var user in userList)
        {
            if (user.Username == username)
            {
                Console.WriteLine("用户名已存在！");
                return false;
            }
        }
        
        User newUser = new User(username, password);
        userList.Add(newUser);
        Console.WriteLine("注册成功！");
        
        Console.WriteLine("当前所有用户：");
        foreach (var user in userList)
        {
            Console.WriteLine($" - {user.Username}");
        }
        
        return true;
    }
    
    public bool Login(string username, string password)
    {
        if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("用户名和密码不可为空！");
            return false;
        }
        
        username = username.Trim();
        password = password.Trim();
        
        foreach (var user in userList)
        {
            if (user.Username == username)
            {
                if (user.Password == password)
                {
                    Console.WriteLine($"登录成功！欢迎 {username}");
                    return true;
                }
                else
                {
                    Console.WriteLine("密码错误！");
                    return false;
                }
            }
        }
        
        Console.WriteLine("用户名不存在！");
        return false;
    }
    
    public void ShowMainMenu()
    {
        Console.WriteLine("\n=== 用户管理系统 ===");
        Console.WriteLine("1. 登录");
        Console.WriteLine("2. 注册");
        Console.WriteLine("3. 显示所有用户");
        Console.WriteLine("4. 删除用户");
        Console.WriteLine("5. 退出");
        Console.Write("请选择操作（1-5）：");
    }
    
    public void HandleUserChoice(int choice)
    {
        switch (choice)
        {
            case 1:
                HandleLogin();
                break;
            case 2:
                HandleRegister();
                break;
            case 3:
                ShowAllUsers();
                break;
            case 4:
                HandleDeleteUser();
                break;
            case 5:
                Console.WriteLine("感谢使用，再见！");
                Environment.Exit(0);
                break;
            default:
                Console.WriteLine("请输入有效数字（1-5）！");
                break;
        }
    }
    
    public List<User> GetAllUsers()
    {
        return new List<User>(userList);
    }
    
    public bool DeleteUser(string username)
    {
        for (int i = 0; i < userList.Count; i++)
        {
            if (userList[i].Username == username)
            {
                userList.RemoveAt(i);
                Console.WriteLine($"用户 {username} 删除成功！");
                return true;
            }
        }
        Console.WriteLine($"用户 {username} 不存在！");
        return false;
    }
    
    public bool ValidateUsername(string username)
    {
        if (username.Length < 3 || username.Length > 15)
        {
            return false;
        }
        
        string usernameRegex = "^[a-zA-Z0-9_]+$";
        return Regex.IsMatch(username, usernameRegex);
    }
    
    public bool ValidatePassword(string password)
    {
        
        if (password.Length < 6 || password.Length > 20)
        {
            return false;
        }
        
        bool hasUpperCase = false;
        bool hasLowerCase = false;
        bool hasDigit = false;
        
        foreach (char c in password)
        {
            if (char.IsUpper(c))
            {
                hasUpperCase = true;
            }
            else if (char.IsLower(c))
            {
                hasLowerCase = true;
            }
            else if (char.IsDigit(c))
            {
                hasDigit = true;
            }
        }
        
        return hasUpperCase && hasLowerCase && hasDigit;
    }
    
    private void HandleLogin()
    {
        Console.WriteLine("\n=== 用户登录 ===");
        Console.Write("请输入用户名：");
        string username = Console.ReadLine();
        Console.Write("请输入密码：");
        string password = Console.ReadLine();
        
        Login(username, password);
    }
    
    private void HandleRegister()
    {
        Console.WriteLine("\n=== 用户注册 ===");
        Console.Write("请输入用户名：");
        string username = Console.ReadLine();
        Console.Write("请输入密码：");
        string password = Console.ReadLine();
        Console.Write("请确认密码：");
        string confirmPassword = Console.ReadLine();
        
        Register(username, password, confirmPassword);
    }
    
    private void ShowAllUsers()
    {
        Console.WriteLine("\n=== 所有用户列表 ===");
        if (userList.Count == 0)
        {
            Console.WriteLine("暂无用户！");
        }
        else
        {
            int count = 1;
            foreach (var user in userList)
            {
                Console.WriteLine($"{count}. {user.Username}");
                count++;
            }
        }
    }
    
    private void HandleDeleteUser()
    {
        Console.WriteLine("\n=== 删除用户 ===");
        ShowAllUsers();
        if (userList.Count > 0)
        {
            Console.Write("请输入要删除的用户名：");
            string username = Console.ReadLine();
            DeleteUser(username);
        }
    }
}